package com.jlastu.znb.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jlastu.znb.R;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.entity.Photo;
import com.jlastu.znb.utils.SpUtils;
import com.jlastu.znb.view.GoodsDetailActivity;
import com.jlastu.znb.view.fkActivity;
import com.jlastu.znb.zj.zjManager;
import java.util.List;

public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.ViewHolder> {

   private List<Goods> mList;
   private Context mContext;
   private zjManager mzjManager;
   private SpUtils mSpUtils;
   public GoodsListAdapter(List<Goods> list,Context context ){
      mList = list;
      mContext=context;
      mzjManager = new zjManager(context);
      mSpUtils = new SpUtils(context);
   }

   public void updateData(List<Goods> list) {
      mList.clear();
      mList = list;
      notifyDataSetChanged();
   }

   @NonNull
   @Override
   public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      Context context = parent.getContext();
      View view=LayoutInflater.from(context).inflate(R.layout.goods_item, parent, false);
      ViewHolder viewHolder = new ViewHolder(view);
      return viewHolder;
   }

   @Override
   public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
      final Goods goods = mList.get(position);
      holder.textView1.setText(goods.getName());
      holder.textView2.setText(String.format("¥ %s/%s", goods.getPrice(), goods.getUnit()));
      List<Photo> photoList = goods.getPhotoList();
      if (photoList != null && photoList.size() > 0) {
         byte[] byteArray = photoList.get(0).getData();
         Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
         holder.photo.setImageBitmap(bitmap);
      } else {
         holder.photo.setImageResource(R.drawable.xiandou);
      }
      holder.itemView.setOnClickListener(v -> {
         // 这里添加将商品添加到足迹的逻辑
         int userId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
         mzjManager.addTozj(goods.getId(),1,userId);
         Intent intentToDetail = new Intent(mContext, GoodsDetailActivity.class);
         intentToDetail.putExtra("GOODS_ID", goods.getId());
         mContext.startActivity(intentToDetail);
         Intent intentToFK = new Intent(mContext, fkActivity.class);
      });
   }

   @Override
   public int getItemCount() {
      return mList.size();
   }

   public static class ViewHolder extends RecyclerView.ViewHolder {
      ImageView photo;
      TextView textView1;
      TextView textView2;

      public ViewHolder(@NonNull View itemView) {
         super(itemView);
         photo = itemView.findViewById(R.id.goods_photo);
         textView1 = itemView.findViewById(R.id.goods_name);
         textView2 = itemView.findViewById(R.id.goods_price);
      }
   }
}
